<?php
class hosting extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hosting_model','host');
	}
	public function load_content(){
		$host_id = @$_POST['host_id'];
		$data = $this->host->get_row($host_id);
		$domain_name = $data['website'];
		$host_package = $data['host_package'];
		$host_expired_date = $data['host_expired_date'];
		$domain_expired_date = $data['domain_expired_date'];
		$ma_start_date = $data['ma_start_date'];
		$ma_end_date = $data['ma_end_date'];
		
				
		if($domain_expired_date > 0){
			$timeStamp = StrToTime($domain_expired_date);
			$domain_regis_start = StrToTime('+1 days', $timeStamp);
			$domain_regis_start = date('Y-m-d', $domain_regis_start);
			$domain_regis_expired = StrToTime('+365 days', $timeStamp);			
			$domain_regis_expired = date('Y-m-d', $domain_regis_expired);
		} 
		if($host_expired_date > 0){
			$timeStamp = StrToTime($host_expired_date);
			$host_regis_start = StrToTime('+1 days', $timeStamp);
			$host_regis_start = date('Y-m-d', $host_regis_start);
			$host_regis_end = StrToTime('+365 days', $timeStamp);			
			$host_regis_end = date('Y-m-d', $host_regis_end);
		}
		 
		$domain_price = $domain_expired_date > 0 ? 350 : 0;
		$host_price = $host_expired_date > 0 ? 1100 : 0;
		$ma_price = $ma_end_date > 0 ? 1000 : 0;
		$total_price = $domain_price + $host_price + $ma_price;
		
		if($domain_expired_date > 0){
			$expired_date  = $domain_expired_date;
		}else if($host_expired_date > 0){
			$expired_date = $host_expired_date;
		}else if($ma_end_date >0){
			$expired_date = $ma_end_date;
		}
		
		$default_message = '
		สวัสดีครับ,
		<br>
		<br>
		'.$domain_name.' จะหมดอายุวันที่ '.mysql_to_date($expired_date,TRUE,'th','/').' <br>
		<br>';

		$default_message .= $domain_expired_date > 0 ?' ต่ออายุโดเมนเนม - '.$domain_name.' - 1 ปี ('.mysql_to_date($domain_regis_start,TRUE,'th','/').' - '.mysql_to_date($domain_regis_expired,TRUE,'th','/').') ฿'.number_format($domain_price,2).' บาท<br>' : "";
		$default_message .= $host_expired_date > 0 ? ' ต่ออายุโฮสติ้ง - '.$host_package.' - 1 ปี ('.mysql_to_date($host_regis_start,TRUE,'th','/').' - '.mysql_to_date($host_regis_end,TRUE,'th','/').') ฿'.number_format($host_price,2).' บาท<br>' : "";
		$default_message .= $ma_end_date > 0 ? ' ค่าดูแลเว็บไซต์  -  ฿'.number_format($ma_price,2).' บาท<br>' : "";
		$default_message .= '<br>
		ยอดรวม ฿'.number_format($total_price,2).' บาท<br>
		<br>
		หมายเลขบัญชีสำหรับการชำระเงิน<br>
		ชื่อบัญชี : นพพร กมลดำรงสกุล   ประเภทบัญชี ออมทรัพย์<br>
		Account Name: Nopporn Kamoldumrongsgul  Saving Account<br>
		 <br>
		<table border="0" cellpadding="0" cellspacing="0">
		  <tbody>
		    <tr bgcolor="#f5f5f5">
		      <td><table border="0" cellpadding="1" cellspacing="1" width="600">
		        <tbody>
		          <tr>
		            <td colspan="4">หมายเลขบัญชีธนาคาร ดังนี้</td>
		          </tr>
		          <tr>
		            <td background="http://imgs/glassgreen.jpg" height="27"><br></td>
		            <td background="http://imgs/glassgreen.jpg" height="27" align="center"><strong>ธนาคาร</strong></td>
		            <td background="http://imgs/glassgreen.jpg" height="27" align="center"><strong>สาขา</strong></td>
		            <td background="http://imgs/glassgreen.jpg" height="27" align="center"><strong>เลขที่บัญชี </strong></td>
		          </tr>
		          <tr>
		            <td bgcolor="white" align="center"><img alt="" src="http://www.tasty-dsign.com/scb.gif" height="18" width="18" /></td>
		            <td bgcolor="white" align="center">ไทยพานิชย์</td>
		            <td bgcolor="white" align="center">เดอะมอลล์ ท่าพระ</td>
		            <td bgcolor="white" align="center">170-2-17398-5</td>
		          </tr>
		          <tr>
		            <td bgcolor="white" align="center"><img alt="" src="http://www.tasty-dsign.com/bbl.gif" height="18" width="18" /></td>
		            <td bgcolor="white" align="center">กรุงเทพ</td>
		            <td bgcolor="white" align="center">คลองสาน</td>
		            <td bgcolor="white" align="center">151-4-05243-8</td>
		          </tr>
		          <tr>
		            <td bgcolor="white" align="center"><img alt="" src="http://www.tasty-dsign.com/kb.gif" height="18" width="18" /></td>
		            <td bgcolor="white" align="center">กสิกรไทย</td>
		            <td bgcolor="white" align="center">คลองสาน</td>
		            <td bgcolor="white" align="center">068-2-82959-1</td>
		          </tr>
		        </tbody>
		      </table></td>
		    </tr>
		  </tbody>
		</table>
		 <br>
		เมื่อชำระเงินแล้วกรุณาแจ้งการชำระเงิน ที่อีเมล์นี้ หรือโทร 084-107-1449<br>
		<br>
		ขอบคุณครับ<br>
		084-107-1449 เอสครับ<br>
		mail / msn : <a href="mailto:spical_4@hotmail.com">spical_4@hotmail.com</a><br>
		website : <a href="http://www.tasty-dsign.com" target="_blank">www.tasty-dsign.com</a><br>
		 <br>
		<img src="http://www.tasty-dsign.com/signature.gif" />		
		';
		echo $default_message;
	}
	public function index()
	{
		//$this->db->debug=true;		
		if(!is_login())redirect('home');
		$data['url_parameter'] = GetCurrentUrlGetParameter();
		$data['host_list'] = $this->host->get(FALSE,TRUE);
		//$data['pagination'] = $this->host->pagination();		
		$this->template->build('index',$data);		
	}
	
	public function save(){
		$url_parameter = GetCurrentUrlGetParameter();
		$_POST['host_expired_date'] = $_POST['host_expired_date'] != '' ? date_to_mysql($_POST['host_expired_date'],TRUE) : "";
		$_POST['domain_expired_date'] = $_POST['domain_expired_date'] != '' ? date_to_mysql($_POST['domain_expired_date'],TRUE) : "";
		$_POST['ma_start_date'] = $_POST['ma_start_date'] != '' ? date_to_mysql($_POST['ma_start_date'],TRUE) : "";
		$_POST['ma_end_date'] = $_POST['ma_end_date'] != '' ? date_to_mysql($_POST['ma_end_date'],TRUE) : "";
		$this->host->save($_POST);
		redirect('hosting/index'.$url_parameter);
	}	
	
	public function delete($ID=FALSE){
		$url_parameter = GetCurrentUrlGetParameter();
		$this->host->delete($ID);
		redirect('hosting/index'.$url_parameter);
	}

	public function send_email(){
		if($_POST){
			$data = $this->host->get_row($_POST['id']);
			$subject = $_POST['subject'];
			$message = $_POST['message'];
			$mail_to = $data['customer_email'];
			
			require_once('include/PHPMailer/class.phpmailer.php');			
					
			$mail             = new PHPMailer(); // defaults to using php "mail()"				
			$body             = $message;
			$body             = eregi_replace("[\]",'',$body);
			
			$mail->AddReplyTo("spical_4@hotmail.com", 'Tasty-dsign.com : ดีไซน์อร่อย');			
			$mail->SetFrom("spical_4@hotmail.com", 'Tasty-dsign.com : ดีไซน์อร่อย');		
			$address = explode(",",$mail_to);
			
			for($i=0;$i<count($address);$i++)
			{
				if($address[$i]!="")$mail->AddAddress($address[$i], "");				
			}
											
			$mail->Subject    = $subject;	
			$mail->CharSet = "utf-8";
			$mail->MsgHTML($body);		
			if(!$mail->Send()) {
			  //echo "Mailer Error: " . $mail->ErrorInfo;
			} else {
			  //echo "Message sent!";
			}
			
			
			;
		}
	}
}
?>